package com.sgb.member.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.member.PickCateType;
import com.sgb.member.R;
import com.sgb.member.databinding.FragmentPriceAdjustment2Binding;
import com.sgb.member.di.DaggerMemberFragComponent;
import com.sgb.member.di.MemberFragModule;
import com.sgb.member.entity.PriceAdjustmentBean;
import com.sgb.member.ui.adapter.PriceAdjustmentAdapter;
import com.sgb.member.view_model.PriceAdjustmentModel2;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.XCustomTabLayout;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.view.selection.CommonPopEntity;
import com.swgk.core.view.selection.MemberStatusView;
import com.swgk.core.xrecyclerview.LoadingMoreFooter;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

public class PriceAdjustmentFragment2 extends AppLazyFragment {

    @Inject
    PriceAdjustmentModel2 sModel;
    private FragmentPriceAdjustment2Binding mBinding;
    private PriceAdjustmentAdapter mAdapter;
    private int mCurPos;
    private String mCurTitle;
    // 数据集
    private final SparseArray<List<CommonPopEntity>> mData = new SparseArray<>();
    private final SparseIntArray mFilter = new SparseIntArray();
    private CommonPopEntity mAll;
    private String mNo1;
    private String mNo2;
    private String mNo3;
    private String mNo4;
    private final String[] mPriceTitle = {"商品调价", "租赁调价", "劳务调价"};

    public static PriceAdjustmentFragment2 newInstance(int position, String title) {
        PriceAdjustmentFragment2 fragment = new PriceAdjustmentFragment2();
        Bundle bundle = new Bundle();
        bundle.putInt("INDEX", position);
        bundle.putString("TITLE", title);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerMemberFragComponent.builder()
                .appComponent(appComponent)
                .memberFragModule(new MemberFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_price_adjustment_2, container, false);
        mBinding.setLifecycleOwner(this);
        return mBinding.getRoot();
    }

    @Override
    protected void initView() {
        mCurPos = getArguments().getInt("INDEX", 0);
        mCurTitle = getArguments().getString("TITLE", "");
        mAll = new CommonPopEntity();
        mAll.setNo("");
        mAll.setTypeName("全部");
        initRecycler();

    }

    @Override
    protected void onLazyLoad() {
        loadData(false);
        loadOtherData();
    }

    public void loadData(boolean isLoadMore) {
        sModel.getPriceAdjustment(mCurPos, isLoadMore, mNo1, mNo2, mNo3, mNo4);
    }

    private void loadOtherData() {
        switch (mCurPos) {
            case 0:
                sModel.getAllCategory(); // 行业分类
                break;
            case 1:
                sModel.getAllLeaseType(); // 行业分类
                break;
            case 2:
                sModel.getAllTeamType(); // 行业分类
                break;
        }
    }

    /**
     * -------------------------------------initRecycler------------------------------------------
     */
    private void initRecycler() {
        mBinding.recyclerView.setPullRefreshEnabled(true);
        mBinding.recyclerView.setLoadingMoreEnabled(true);
        LoadingMoreFooter footer = (LoadingMoreFooter) mBinding.recyclerView.getFootView();
        footer.setNoMoreHint("已经到底啦~");
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                loadData(false);
            }

            @Override
            public void onLoadMore() {
                loadData(true);
            }
        });
        mAdapter = new PriceAdjustmentAdapter(getActivity(), null, mCurPos, mCurTitle);
        mBinding.recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClick(new AdapterOnItemClick<PriceAdjustmentBean>() {
            @Override
            public void onItemClick(PriceAdjustmentBean bean, int position) {
                sModel.getUrlSetting(mCurPos, bean);

            }
        });
    }

    public void closeRecyclerRefreshing() {
        mBinding.recyclerView.refreshComplete();
        mBinding.recyclerView.loadMoreComplete();
    }

    public void showEmpty() {
        mBinding.recyclerView.setVisibility(View.GONE);
        mBinding.empView.setVisibility(View.VISIBLE);
    }

    public void refreshRecycler(List<PriceAdjustmentBean> list, boolean isLoadMore, boolean noMoreData) {
        mBinding.recyclerView.setVisibility(View.VISIBLE);
        mBinding.recyclerView.setLoadingMoreEnabled(!noMoreData);
        closeRecyclerRefreshing();
        if (isLoadMore) {
            if (list != null && list.size() > 0) {
                mAdapter.addDatas(list);
            }
        } else {
            if (list != null && list.size() > 0) {
                mBinding.empView.setVisibility(View.GONE);
                mAdapter.setDatas(list);
            } else {
                mAdapter.clearDatas();
                mBinding.empView.setVisibility(View.VISIBLE);
            }
        }
    }


//
//    @Override
//    public void onChooseResult(String mKeyName, CommonPopEntity entity, Object... objects) {
//        int position = (int) objects[0];
//        String no = position == 0 ? "" : entity.getNo();
//        String id = position == 0 ? "" : entity.getId();
//        switch (mCurPos) {
//            case 0: // 商品调价
//                switch (mKeyName) {
//                    case "分类":
//                        if (!TextUtils.equals(id, mNo1)) {
//                            mNo1 = id;
//                            updateBusinessOtherData(PickCateType.TYPE_1, position);
//                            loadData(false);
//                        }
//                        break;
//                    case "类别":
//                        if (!TextUtils.equals(id, mNo2)) {
//                            mNo2 = id;
//                            updateBusinessOtherData(PickCateType.TYPE_2, position);
//                            loadData(false);
//                        }
//                        break;
//                    case "品名":
//                        if (!TextUtils.equals(id, mNo3)) {
//                            mNo3 = id;
//                            updateBusinessOtherData(PickCateType.TYPE_3, position);
//                            loadData(false);
//                        }
//                        break;
//                }
//                break;
//            case 1: // 租赁调价
//                switch (mKeyName) {
//                    case "类目":
//                        if (!TextUtils.equals(no, mNo1)) {
//                            mNo1 = no;
//                            updateBusinessOtherData(PickCateType.TYPE_1, position);
//                            loadData(false);
//                        }
//                        break;
//                    case "分类":
//                        if (!TextUtils.equals(no, mNo2)) {
//                            mNo2 = no;
//                            updateBusinessOtherData(PickCateType.TYPE_2, position);
//                            loadData(false);
//                        }
//                        break;
//                    case "类型":
//                        if (!TextUtils.equals(no, mNo3)) {
//                            mNo3 = no;
//                            updateBusinessOtherData(PickCateType.TYPE_3, position);
//                            loadData(false);
//                        }
//                        break;
//                }
//                break;
//            case 2: // 劳务调价
//                switch (mKeyName) {
//                    case "工程分类":
//                        if (!TextUtils.equals(no, mNo1)) {
//                            mNo1 = no;
//                            updateBusinessOtherData(PickCateType.TYPE_1, position);
//                            loadData(false);
//                        }
//                        break;
//                    case "工程专项":
//                        if (!TextUtils.equals(no, mNo2)) {
//                            mNo2 = no;
//                            updateBusinessOtherData(PickCateType.TYPE_2, position);
//                            loadData(false);
//                        }
//                        break;
//                    case "专业分类":
//                        if (!TextUtils.equals(no, mNo3)) {
//                            mNo3 = no;
//                            updateBusinessOtherData(PickCateType.TYPE_3, position);
//                            loadData(false);
//                        }
//                        break;
//                    case "工种名称":
//                        if (!TextUtils.equals(no, mNo4)) {
//                            mNo4 = no;
//                            loadData(false);
//                        }
//                        break;
//                }
//                break;
//        }
//    }

    public void setTablayout1(List<CommonPopEntity> data) {
        mBinding.tabLayout1.removeAllTabs();
        mBinding.tabLayout1.clearOnTabSelectedListeners();
        mBinding.tabLayout1.setNormalColor(R.color.color_title_small);
        mBinding.tabLayout1.setSelectColor(R.color.color_main);
        mBinding.tabLayout1.setNormalSize(14);
        mBinding.tabLayout1.setSelectSize(14);
        mBinding.tabLayout1.setTabMode(TabLayout.MODE_SCROLLABLE);
        for (int i = 0; i < data.size(); i++) {
            mBinding.tabLayout1.addTab(mBinding.tabLayout1.newTab().setText(data.get(i).getTypeName()));
        }
        mBinding.tabLayout1.addOnTabSelectedListener(data, new XCustomTabLayout.SimpleSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mNo1 = data.get(tab.getPosition()).getId();
                mNo2 = "";
                mNo3 = "";
                sModel.getCategoryChoose(tab.getPosition());
                loadData(false);
            }
        });
    }

    public void setTablayout2(List<CommonPopEntity> data) {
        if (data.size() > 0) {
            mBinding.tabLayout2.setVisibility(View.VISIBLE);
        } else {
            mBinding.tabLayout2.setVisibility(View.GONE);
            return;
        }
        mBinding.tabLayout2.removeAllTabs();
        mBinding.tabLayout2.clearOnTabSelectedListeners();
        mBinding.tabLayout2.setNormalColor(R.color.color_title_small);
        mBinding.tabLayout2.setSelectColor(R.color.color_main);
        mBinding.tabLayout2.setNormalSize(14);
        mBinding.tabLayout2.setSelectSize(14);
        mBinding.tabLayout2.setTabMode(TabLayout.MODE_SCROLLABLE);
        for (int i = 0; i < data.size(); i++) {
            mBinding.tabLayout2.addTab(mBinding.tabLayout2.newTab().setText(data.get(i).getTypeName()));
        }
        mBinding.tabLayout2.addOnTabSelectedListener(data, new XCustomTabLayout.SimpleSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mNo2 = data.get(tab.getPosition()).getId();
                mNo3 = "";
                sModel.getCateArguments(tab.getPosition());
                loadData(false);
            }
        });
    }


    public void setTablayout3(List<CommonPopEntity> data) {
        if (data.size() > 0) {
            mBinding.tabLayout3.setVisibility(View.VISIBLE);
        } else {
            mBinding.tabLayout3.setVisibility(View.GONE);
            return;
        }
        mBinding.tabLayout3.removeAllTabs();
        mBinding.tabLayout3.clearOnTabSelectedListeners();
        mBinding.tabLayout3.setNormalColor(R.color.color_title_small);
        mBinding.tabLayout3.setSelectColor(R.color.color_main);
        mBinding.tabLayout3.setNormalSize(14);
        mBinding.tabLayout3.setSelectSize(14);
        mBinding.tabLayout3.setTabMode(TabLayout.MODE_SCROLLABLE);
        for (int i = 0; i < data.size(); i++) {
            mBinding.tabLayout3.addTab(mBinding.tabLayout3.newTab().setText(data.get(i).getTypeName()));
        }
        mBinding.tabLayout3.addOnTabSelectedListener(data, new XCustomTabLayout.SimpleSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mNo2 = data.get(tab.getPosition()).getNo();
                loadData(false);
            }
        });
    }


}
